use "$project_path/data/4_regdata/regdata", clear

egen diff_m1_chix_govint = rowmean(std_diff_m1_exp_gdp std_diff_m1_tax_revenue_wb std_diff_m1_debt_imf)

log using "$project_path/output/appendix_tables/Appendix_Table_E7.smcl", replace

rdrobust std_diff_m1_exp_gdp runvar, all
rdrobust std_diff_m1_tax_revenue_wb runvar, all
rdrobust std_diff_m1_debt_imf runvar, all
rdrobust diff_m1_chix_govint runvar, all
foreach v in exp_educ_gdp exp_health_gdp exp_mili_gdp subsidies_transf cbi_cwn cb_policy_rate share_educ_exp share_health_exp share_mili_exp share_tax_goods share_tax_income share_tax_trade finreform dircredit creditceil creditcont ircontr entrybar banksup privat intcap secmarkets econ_freedom trade_lib {
	rdrobust std_diff_m1_`v' runvar, all
}

log close

*** Building .tex table
// Regressions 
#delimit ;
local outcomes  
		std_diff_m1_exp_gdp std_diff_m1_tax_revenue_wb std_diff_m1_debt_imf diff_m1_chix_govint std_diff_m1_exp_educ_gdp std_diff_m1_exp_health_gdp std_diff_m1_exp_mili_gdp std_diff_m1_subsidies_transf std_diff_m1_cbi_cwn std_diff_m1_cb_policy_rate std_diff_m1_share_educ_exp std_diff_m1_share_health_exp std_diff_m1_share_mili_exp std_diff_m1_share_tax_goods std_diff_m1_share_tax_income std_diff_m1_share_tax_trade std_diff_m1_finreform std_diff_m1_dircredit std_diff_m1_creditceil std_diff_m1_creditcont std_diff_m1_ircontr std_diff_m1_entrybar std_diff_m1_banksup std_diff_m1_privat std_diff_m1_intcap std_diff_m1_secmarkets std_diff_m1_econ_freedom std_diff_m1_trade_lib;
#delimit cr		

// Initialize matrix of results and define value labels
local ncols = 5
local nrows = 28
//Prepare matrix of coefficients and other statistics
mat Coefs = J(`nrows', `ncols', .)

mat list Coefs

//Inizialize indices
local row = 0

foreach v in `outcomes'{
	//Initialize indices
	local ++row

	rdrobust `v' runvar, all
	
	//Store estimates and stats
	local b_`row': di %5.3f e(tau_cl)
	local se_`row': di %5.3f e(se_tau_rb)
	local p_`row': di %5.3f e(pv_rb)
	local N_`row': di %6.0f e(N)

	mat Coefs[`row',1] = `b_`row''
	mat Coefs[`row',2] = `se_`row''
	mat Coefs[`row',3] = `p_`row''
	mat Coefs[`row',4] =  `N_`row''
	mat Coefs[`row',5] = .
	
	// Extract and store sources
    local basevar = "`v'"
	local basevar = subinstr("`basevar'", "mstd_", "", .)
	local basevar = subinstr("`basevar'", "std_", "", .)
	local basevar = subinstr("`basevar'", "diff_m1_", "", .)
	local source`row' = subinstr("``basevar'[note1]'", "Source - ", "", .)

}
mat list Coefs

********************************************************************************
* 								EXPORT TABLE
********************************************************************************			
	// Clear the dataset, and use the matrix as dataset			

	clear
	svmat Coefs

	//	input variable names
	gen varname = ""
	gen temp = _n
	replace varname = "Government expenditure (\% of GDP)" if temp == 1
	replace varname = "Tax revenue (\% of GDP)" if temp == 2
	replace varname = "Debt (\% of GDP)" if temp == 3
	replace varname = "Government intervention index" if temp == 4
	replace varname = "Education expenditure (\% of GDP)" if temp == 5
	replace varname = "Public health expenditure (\% of GDP)" if temp == 6
	replace varname = "Military expenditure (\% of GDP)" if temp == 7
	replace varname = "Subsidies and transfers (\% of exp.)" if temp == 8
	replace varname = "Central bank independence" if temp == 9
	replace varname = "Central bank policy rate" if temp == 10
	replace varname = "Educ. expenditure (\% of exp.)" if temp == 11
	replace varname = "Health expenditure (\% of exp.)" if temp == 12
	replace varname = "Military expenditure (\% of exp.)" if temp == 13
	replace varname = "Taxes on goods (\% of taxes)" if temp == 14
	replace varname = "Taxes on income (\% of taxes)" if temp == 15
	replace varname = "Taxes on trade (\% of taxes)" if temp == 16
	replace varname = "Financial liberlization index" if temp == 17
	replace varname = "Directed credit liberalization" if temp == 18
	replace varname = "Credit ceilings liberalization" if temp == 19
	replace varname = "Credit controls liberalization" if temp == 20
	replace varname = "Interest rates liberalization" if temp == 21
	replace varname = "Pro-competitive measures" if temp == 22
	replace varname = "Banking supervision liberalization" if temp == 23
	replace varname = "Privatization measures" if temp == 24
	replace varname = "International capital flows liberalization" if temp == 25
	replace varname = "Security market liberalization" if temp == 26
	replace varname = "Economic freedom index" if temp == 27
	replace varname = "Liberalized trade" if temp == 28
	
	order varname, first
	drop temp
	
	// Rename cols and labels
	forvalues n = 1/5{
		rename Coefs`n' Col`n'
	}
	
	label var varname ""
	label var Col1 "Est."
	label var Col2 "SE"
	label var Col3 "p-val"
	label var Col4 "N"
	label var Col5 "Source"
	
		
	// Convert to string and add options
	foreach i in 1 2 4 5{
		tostring Col`i', replace force format(%04.3f)
		replace Col`i'= "" if Col`i'=="."
	}
	
	// Sources
	forvalues i =1/`nrows' {
		replace Col5 = "`source`i''" in `i'
	}
	
	//Remove decimals to N obs
	replace Col4 = substr(Col4, 1, strpos(Col4, ".")-1)
		
	// Add stars
	local 3_star 0.01
	local 2_star 0.05
	local 1_star 0.10
	
	replace Col1 = Col1 + "***" if Col3 < `3_star'
	replace Col1 = Col1 + "**" if Col3 < `2_star' & Col3 > `3_star'
	replace Col1 = Col1 + "*" if Col3 < `1_star' & Col3 > `2_star'
	
    tostring Col3, replace force format(%04.3f)
    replace Col3= "" if Col3=="."
	
	// Add parenthesis to SE
	replace Col2 = "(" + Col2 + ")" if Col2 != ""
	
	// Add brackets to p-values
	replace Col3 = "[" + Col3 + "]" if Col3 != ""

	foreach var of varlist _all {
	qui replace `var' = ustrtrim(`var')	
}

local title = "Effects on additional policy variables"
local filename = "policy_variables_baseline"
local output "$project_path/output/appendix_tables/Appendix_Table_E7.tex"
local footnote = "\caption*{\footnotesize \emph{Notes}: This table reports RD estimates corresponding to equation (\ref{eq:baseline_rdd}) for measures of policy, expressed in standard deviation terms.}"


	texsave using "`output'", replace title("`title'") varlabels noendash ///
							  footnote("`footnote'") align(p{6.5cm} p{1.6cm} p{1.6cm} p{1.6cm} p{1.6cm} p{3.3cm}) ///
							  frag nofix marker("tab:`filename'") location(H)
